<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>拖动元素</title>
		<link rel="stylesheet" type="text/css" href="css/mobbile.css"/>
		<link rel="stylesheet" type="text/css" href="css/normalize.css"/>
		<link rel="stylesheet" type="text/css" href="css/index.css"/>
		<style type="text/css">
			.box {
				position: absolute;
				top: 0;
				left: 0;
				width: 200px;
				height: 200px;
				background-color: pink;
			}
		</style>
	</head>
	<body>
		<div class="box"></div>
		<script type="text/javascript">
			//通过点击时，原来的盒子的坐标 + 触摸点在移动过程中的距离
			var div = document.querySelector('div');
			
			//触摸点的坐标
			var starX = 0;
			var startY = 0;
			//盒子的坐标
			var x = 0;
			var y = 0;
			div.addEventListener('touchstart',function(e) {
				console.log(e.targetTouches[0])
				//刚开始触摸点的坐标
				startX = e.targetTouches[0].pageX;
				startY = e.targetTouches[0].pageY;
				//盒子的坐标
				x = this.offsetLeft;
				y = this.offsetTop;
			});
			//触摸点移动
			div.addEventListener('touchmove',function(e) {
				//获取鼠标移动的距离
				var moveX = e.targetTouches[0].pageX - startX;
				var moveY = e.targetTouches[0].pageY - startY;
				//设置盒子
				div.style.left = x + moveX + 'px';
				div.style.top = y + moveY + 'px';
			})
		</script>
	</body>
</html>
